﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ActionSystem.Input
{
    /// <summary>
    /// 打印信息
    /// </summary>
    public class InputUtil : MonoBehaviour
    {
        private static InputUtil _instance;
        public static InputUtil Instance => _instance;
        private Dictionary<string,InputModule> inputs = new Dictionary<string, InputModule>();
        private Dictionary<string, Type> inputTypes = new Dictionary<string, Type>();
        public void AddInputModule(string name,InputModule input,Type  keyEnum=null)
        {
            if(!inputs.ContainsKey(name))
            {
                inputs.Add(name,input);
            }
            if (!inputTypes.ContainsKey(name))
            {
                inputTypes.Add(name, keyEnum);
            }
        }
        public void RemoveInputModule(string name)
        {
            if (inputs.ContainsKey(name))
            {
                inputs.Remove(name);
            }
            if (inputTypes.ContainsKey(name))
            {
                inputTypes.Remove(name);
            }
        }
        private void Awake()
        {
            _instance = this;
        }

        private void OnGUI()
        {
            GUI.color = Color.red;
            GUILayout.Space(100);            
            foreach(var input in inputs.Keys)
            {
                GUILayout.BeginVertical();
                GUILayout.Label("============================================================");
                GUILayout.Label(input);
                GUILayout.Label("------------------------------------------------------------");
                foreach (var buffer in inputs[input].Keys)
                {
                    if(buffer.Value.Type!=ButtonType.Button)
                    {
                        continue;
                    }
                    GUILayout.BeginHorizontal();
                    if(inputTypes[input]!=null)
                    {                                                
                        GUILayout.Label($"{Enum.GetName(inputTypes[input], buffer.Key)}|");
                    }
                    else
                    {
                        GUILayout.Label($"{buffer.Key}|");
                    }
                    
                    foreach(var v in buffer.Value.Cache)
                    {                        
                        GUILayout.Label($"{(v==null?"X":v.State==ButtonState.Up?"1":v.State==ButtonState.Down?"0":"-")} ");
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.Label("------------------------------------------------------------");
                }
                foreach (var buffer in inputs[input].Keys)
                {
                    if (buffer.Value.Type != ButtonType.Stick)
                    {
                        continue;
                    }
                    GUILayout.BeginHorizontal();
                    if (inputTypes[input] != null)
                    {
                        GUILayout.Label($"{Enum.GetName(inputTypes[input], buffer.Key)}|");
                    }
                    else
                    {
                        GUILayout.Label($"{buffer.Key}|");
                    }
                    foreach (var v in buffer.Value.Cache)
                    {
                        GUILayout.Label($"{v.Stick} ");
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.Label("------------------------------------------------------------");
                }
                GUILayout.EndVertical();
                
            }
            GUI.color = Color.white;
        }


    }
}

